(function($){
    $.fn.TimeSel=function(options){
        var defaults = {
         selected:"selected" 
       };
      // Extend our default options with those provided.
        var opts = $.extend(defaults, options);
        
        var timeDate;
        var tabHtml='<table class="timetab">'+
                               '<thead>'+
                                 '<th colspan="2"></th>'+
                                 '<th colspan="2">00:00</th>'+
                                 '<th colspan="2">02:00</th>'+
                                 '<th colspan="2">04:00</th>'+
                                 '<th colspan="2">06:00</th>'+
                                 '<th colspan="2">08:00</th>'+
                                 '<th colspan="2">10:00</th>'+
                                 '<th colspan="2">12:00</th>'+
                                 '<th colspan="2">14:00</th>'+
                                 '<th colspan="2">16:00</th>'+
                                 '<th colspan="2">18:00</th>'+
                                 '<th colspan="2">20:00</th>'+
                                 '<th colspan="2">22:00</th>'+
                               '</thead>'+
                               '<tbody>'+
                                 '</tbody>'+
                               '<tfoot>'+
                                 '<tr>'+
                                     '<td colspan="2">订单类型：</td>'+
                                     '<td colspan="16">'+
                                       '<a id="allday" href="javascript:;" class="btn-gray btn-heith-sm">全周投放</a>&nbsp;&nbsp;'+
                                       '<a id="workday" href="javascript:;" class="btn-gray btn-heith-sm">工作日投放</a>&nbsp;&nbsp;'+
                                       '<a id="weekend" href="javascript:;" class="btn-gray btn-heith-sm">周末投放</a>&nbsp;&nbsp;'+
                                       '<a id="resetbtn" href="javascript:;" class="btn-gray btn-heith-sm">重置</a>'+
                                       '</td>'+
                                     '<td colspan="1"><i class="put"></i></td>'+
                                     '<td colspan="3">投放时段</td>'+
                                     '<td colspan="1"><i class="no-put"></i></td>'+
                                     '<td colspan="3">非投放时段</td>'+
                                 '</tr>'+
                               '</tfoot>'+
                             '</table>';
       var methods = {
        clearUnit: function (opts,obj,c) {
            var unit=obj.find('.time-unit');
            var input=obj.find('input[type=checkbox]');
            
            unit.each(function(){
              $(this).removeClass(c)
            });
            input.prop('checked',false);
        },

        };
        return this.each(function() {
            var $this=$(this);
            var weekday=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];
            var clock=["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"];
            $this.append(tabHtml);
            $tab=$("table", $this);
            var n;
            for(var i=0;i<weekday.length;i++){
            var tr=$("<tr></tr>");
            
            var html='<td>'+
               '<label class="position-relative">'+
               '<input type="checkbox" class="ace">'+
               '<span class="lbl"></span>'+
               '</label>'+
               '</td>'+
              '<td class="pdr10">'+weekday[i]+'</td>';
              for(var j=0;j<clock.length;j++){
                n=i*24+j;
                html+= '<td class="time-unit" data-id="'+n+'" data-time="'+clock[j]+'"></td>';
              }
              tr.append(html);
              $tab.append(tr);
            }
          
           
            var flag;
               $this.on('mousedown','.time-unit',function(e){
                flag=true;
               if(e.preventDefault){
                e.preventDefault();
                }else{
                window.event.returnValue == false;
                }
                 
             $(".time-unit").mousemove(function(e){
              if(flag){
                 $(this).addClass(opts.selected);
                
              }
             
              $(".time-unit").mouseup(function(e){
                flag=false;
                 
               $(".time-unit").unbind('mousemove');
              })
              $tab.mouseleave(function(e) {
                flag=false;
               $(".time-unit").unbind('mousemove');
              });
             })
            })
               .on('click','.time-unit',function(e){
                 flag=false;
              
                  if($(this).hasClass(opts.selected)){
                $(this).removeClass(opts.selected);
                 }else{
                $(this).addClass(opts.selected);
                  }
               })
               .on('click','input[type=checkbox]',function(){
                  var par=$(this).parents('tr');
                  if($(this).is(':checked')){
                  par.find('.time-unit').addClass(opts.selected);
                  }else{
                  par.find('.time-unit').removeClass(opts.selected);

                 }
        
                 })
                 .on('click','#allday',function(){
                  methods['clearUnit'](opts,$tab,opts.selected);
                  $tab.find('.time-unit').addClass(opts.selected);
                  $tab.find('input[type=checkbox]').prop('checked',true);
                  
                 }) 
                 .on('click','#workday',function(){
                     methods['clearUnit'](opts,$tab,opts.selected);
                     for(var i=0;i<5;i++){
                      $tab.find('tbody tr').eq(i).find('.time-unit').addClass(opts.selected);
                      $tab.find('input[type=checkbox]').eq(i).prop('checked',true);
                     }
                 }) 
                 .on('click','#weekend',function(){
                     methods['clearUnit'](opts,$tab,opts.selected);
                     $tab.find('tbody tr').eq(5).find('.time-unit').addClass(opts.selected);
                     $tab.find('tbody tr').eq(6).find('.time-unit').addClass(opts.selected);
                    $tab.find('input[type=checkbox]').eq(5).prop('checked',true);
                    $tab.find('input[type=checkbox]').eq(6).prop('checked',true);
                 })
                 .on('click','#resetbtn',function(){
                     methods['clearUnit'](opts,$tab,opts.selected);
                 });   
        });
         
    };

   
  

})(jQuery);